package flyway

import (
	"encoding/json"
	"time"
)

var flywaySchemaHistorySql = "CREATE TABLE IF NOT EXISTS `%s` (`installed_rank` int(11) NOT NULL,`version` varchar(50) DEFAULT NULL,`description` varchar(200) NOT NULL,`type` varchar(20) NOT NULL,`script` varchar(1000) NOT NULL,`checksum` varchar(50) DEFAULT NULL,`installed_by` varchar(100) NOT NULL,`installed_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,`execution_time` int(11) NOT NULL,`success` tinyint(1) NOT NULL,PRIMARY KEY (`installed_rank`),KEY `flyway_schema_history_s_idx` (`success`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"

type FlywaySchemaHistory struct {
	InstalledRank int        `gorm:"primaryKey;column:installed_rank;type:int(11);not null" json:"installedRank"`
	Version       string     `gorm:"column:version;type:varchar(50)" json:"version"`
	Description   string     `gorm:"column:description;type:varchar(200);not null" json:"description"`
	Type          string     `gorm:"column:type;type:varchar(20);not null" json:"type"`
	Script        string     `gorm:"column:script;type:varchar(1000);not null" json:"script"`
	Checksum      string     `gorm:"column:checksum;type:varchar(50)" json:"checksum"`
	InstalledBy   string     `gorm:"column:installed_by;type:varchar(100);not null" json:"installedBy"`
	InstalledOn   *time.Time `gorm:"column:installed_on;type:timestamp;not null;default:CURRENT_TIMESTAMP" json:"installedOn"`
	ExecutionTime int        `gorm:"column:execution_time;type:int(11);not null" json:"executionTime"`
	Success       bool       `gorm:"index:flyway_schema_history_s_idx;column:success;type:tinyint(1);not null" json:"success"`
}

func (f FlywaySchemaHistory) String() string {
	bytes, _ := json.Marshal(f)
	return string(bytes)
}
